'''
获取到所有的PR信息，找出每一个PR创建的时间，以及在该PR创建时间那个时刻仍处于open状态的pr，
然后将这个时刻还处于open状态的pr作为输入X。
FIFO算法，根据pr创建的时间先创建，放在最前面，这样对上述pr列表进行排序。FIFOY
真实排序：在该时刻之后，该X中，被相应，或者被关闭或者被合并等发生改变的时间，根据该时间顺序进行排序，进而获取真实排序TRUEY
将FIFOY，与TRUEY进行比较，通过NDcg进行比较，判断排序效果
'''
import os
# 增加代码的可读性
from utils.path_exist import path_exists_or_create
import time
import subprocess


# 训练模型
def train_model(alg_name, alg_index, train_data_path, test_data_path, model_path, jar_path):
    if alg_name == "RankBoost":
        rank_model_str = "java -Xms14000m -Xmx200042m -Xmn700m -Xss16m -jar " + jar_path + " -train " + train_data_path + " -test " + test_data_path + " -ranker " + str(
            alg_index) \
                         + " -layer 2" + " -gmax 12" + " -metric2t NDCG@10 -metric2T NDCG@10 -lr 0.00001 -save " + model_path
    elif alg_name =="LambdaMART":
        # -norm zscore -shrinkage 0.0001 -tree 500 -leaf 100 -mls 10 -gmax 12
        # java -jar ./RankLib-2.16.jar -load ./rank_model/salt/salt_LambdaMART_model.txt -rank ./rank_data/salt/salt_svm_rank_format_year_data.txt -gmax 12 -norm zscore -indri ./rank_model/salt/result/salt_result_LambdaMART.txt
        rank_model_str = "java -Xms14000m -Xmx200042m -Xmn700m -Xss16m -jar " + jar_path + " -train " + train_data_path + " -test " + test_data_path + " -ranker " + str(
            alg_index) \
                         + " -layer 2" + " -shrinkage 0.0001  -tc -1 -leaf 50 -mls 10 -gmax 12 " + " -metric2t NDCG@10 -metric2T NDCG@10 -lr 0.00001 -save " + model_path
    else:
        rank_model_str = "java -jar " + jar_path + " -train " + train_data_path + " -test " + test_data_path + " -ranker " + str(
            alg_index) \
                         + " -layer 2" + " -gmax 12 " + " -metric2t NDCG@10 -metric2T NDCG@10 -lr 0.00001 -save " + model_path
    # recv = os.popen(rank_model_str)
    print("===============训练模型+" + alg_name + "======================")
    print("训练的命令是：" + rank_model_str)
    # print(recv.read())
    start_time = time.time()
    pipe = subprocess.Popen(rank_model_str, shell=True, stdout=subprocess.PIPE, bufsize=1)

    # 实时打印log
    def print_log():
        for info in iter(pipe.stdout.readline, b''):
            print(info)
            # 10s后关闭子进程
            if time.time() - start_time > 100000:
                pipe.terminate()
                pipe.wait()
                break

    print_log()


def run_rankLib_baseline(repo_name, alg_name, alg_index):
    # 测试模型性能的文件路径
    jar_path = "./RankLib-2.16.jar"
    file_path = "./rank_data/" + repo_name + "/"
    path_exists_or_create(file_path)

    model_path = "./rank_model/" + repo_name + "/"
    path_exists_or_create(model_path)
    model_path = model_path + repo_name + "_" + alg_name + "_model.txt"
    # 训练模型的文件路径
    train_data_path = file_path + repo_name + "_svm_rank_format_train_data.txt"
    test_data_path = file_path + repo_name + "_svm_rank_format_test_data.txt"
    # 首先运行算法训练模型
    train_model(alg_name, alg_index, train_data_path, test_data_path, model_path, jar_path)
    print(alg_name + "模型训练完成==========")
    # 输出测试集结果
    test_sort_result_path = "./rank_model/" + repo_name + "/result/"
    path_exists_or_create(test_sort_result_path)
    test_sort_result_path = test_sort_result_path + repo_name + "_result" + "_" + alg_name + ".txt"
    year_filename = file_path + repo_name + "_svm_rank_format_year_data.txt"

    test_rank_str = "java -jar " + jar_path + " -load " + model_path + " -rank " + year_filename + " -norm zscore -gmax 12" + " -indri " + test_sort_result_path
    testcv = os.popen(test_rank_str)
    print(testcv.read())
    print(test_rank_str)
    col_index = 2
    import pandas as pd

    df = pd.read_table(test_sort_result_path, sep=' ', header=None)
    df = df.sort_values(by=col_index)
    test_sort_result_path = test_sort_result_path[:test_sort_result_path.find('.txt')]
    df.to_excel(f'{test_sort_result_path}_sorted.xlsx', index=False)


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    repo_list = ["kubernetes"]  # "laravel","angular.js"
    for repo_name in repo_list:
        # repo_name ="tensorflow"#storm"#"scikit-learn"#"moby"#"cocos2d-x"#"netbeans"#"yii2"##"dubbo"#"react"#"tensorflow"#"opencv"#"phoenix"#"helix"#"terraform"#"Ipython"#"kuma"#"incubator-heron"#"Katello"#"zipkin"#"incubator-heron"# "Katello"#"zipkin"#"yii2"#"dubbo"#"react"#"tensorflow"# "opencv"#"phoenix"#"guacamole-client"# "helix"#"terraform"#"Ipython"#"kuma"#"incubator-heron"#"Katello" #"salt"  # "zipkin"#"angular.js"  # "symfony"# #"tensorflow"#"spring-boot"#"spring-framework"#"rails"
        # ranklib所能调的库
        alg_dict = {
            # 0: "MART",
            # 1: "RankNet",
            # 2: "RankBoost",
            # 3: "AdaRank",
            # 4: "Coordinate_Ascent",
            # 6: "LambdaMART",
            7: "ListNet",
            # 8: "Random_Forests"
        }
        for alg_index in alg_dict.keys():
            alg_name = alg_dict.get(alg_index)
            # 测试模型性能的文件路径
            jar_path = "./RankLib-2.16.jar"
            file_path = "./rank_data/" + repo_name + "/"
            path_exists_or_create(file_path)
            origin_data_path = file_path + repo_name + "_svm_rank_format_test_data.txt"
            temp_data_path = file_path + repo_name + "_temp_svm_rank_format_data.txt"
            temp_sort_result_path = file_path + repo_name + "_myScoreFile.txt"

            model_path = "./rank_model/" + repo_name + "/"
            path_exists_or_create(model_path)
            model_path = model_path + repo_name + "_" + alg_name + "_model.txt"
            # 训练模型的文件路径
            train_data_path = file_path + repo_name + "_svm_rank_format_train_data.txt"
            test_data_path = file_path + repo_name + "_svm_rank_format_test_data.txt"
            # 首先运行算法训练模型
            train_model(alg_name, alg_index, train_data_path, test_data_path, model_path, jar_path)
            print(alg_name + "模型训练完成==========")

            # 输出测试集结果
            test_sort_result_path = "./rank_model/" + repo_name + "/result/"
            path_exists_or_create(test_sort_result_path)
            test_sort_result_path = test_sort_result_path + repo_name + "_result" + "_" + alg_name + ".txt"
            year_filename = file_path + repo_name + "_svm_rank_format_year_data.txt"

            test_rank_str = "java -jar " + jar_path + " -load " + model_path + " -rank " + year_filename + " -gmax 12" + " -indri " + test_sort_result_path
            testcv = os.popen(test_rank_str)
            print(testcv.read())
            print(test_rank_str)
            col_index = 2
            import pandas as pd

            df = pd.read_table(test_sort_result_path, sep=' ', header=None)
            df = df.sort_values(by=col_index)
            test_sort_result_path = test_sort_result_path[:test_sort_result_path.find('.txt')]
            df.to_excel(f'{test_sort_result_path}_sorted.xlsx', index=False)


#java -jar ./RankLib-2.16.jar -train ./rank_data/cmssw/cmssw_svm_rank_format_train_data.txt -test ./rank_data/cmssw/cmssw_svm_rank_format_test_data.txt -ranker 6 -layer 2 -norm zscore -shrinkage 0.0001 -tree 500 -tc -1 -leaf 100 -mls 10 -gmax 12  -metric2t NDCG@10 -metric2T NDCG@10 -lr 0.00001 -save ./rank_model/cmssw/cmssw_LambdaMART_model.txt
#java -jar ./RankLib-2.16.jar -load ./rank_model/cmssw/cmssw_LambdaMART_model.txt -rank ./rank_data/cmssw/cmssw_svm_rank_format_year_data.txt -gmax 12 -norm zscore -indri ./rank_model/cmssw/result/cmssw_result_LambdaMART.txt

# java -jar ./RankLib-2.16.jar -train ./rank_data/cmssw/cmssw_svm_rank_format_train_data.txt -test ./rank_data/cmssw/cmssw_svm_rank_format_test_data.txt -ranker 7 -epoch 500  -metric2t ERR@5 -lr 0.001  -norm zscore -gmax 12  -save ./rank_model/cmssw/cmssw_ListNet_model.txt
#java -jar ./RankLib-2.16.jar -load ./rank_model/cmssw/cmssw_ListNet_model.txt -rank ./rank_data/cmssw/cmssw_svm_rank_format_year_data.txt -gmax 12 -norm zscore -indri ./rank_model/cmssw/result/cmssw_result_ListNet.txt
#
#java -jar ./RankLib-2.16.jar -train ./rank_data/tensorflow/tensorflow_svm_rank_format_train_data.txt -test ./rank_data/tensorflow/tensorflow_svm_rank_format_test_data.txt -ranker 3  -max 20 -tolerance 0.002   -norm sum -gmax 12 -save ./rank_model/tensorflow/tensorflow_AdaRank_model.txt
#java -jar ./RankLib-2.16.jar -load ./rank_model/tensorflow/tensorflow_AdaRank_model.txt -rank ./rank_data/tensorflow/tensorflow_svm_rank_format_year_data.txt -gmax 12 -norm sum -indri ./rank_model/tensorflow/result/tensorflow_result_AdaRank.txt


# java -Xms24000m -Xmx200042m  -jar ./RankLib-2.16.jar -train ./rank_data/kubernetes/kubernetes_svm_rank_format_train_data.txt -test ./rank_data/kubernetes/kubernetes_svm_rank_format_test_data.txt -ranker 2 -layer 2  -gmax 12  -metric2t NDCG@10 -metric2T NDCG@10 -lr 0.00001 -save ./rank_model/kubernetes/kubernetes_RankBoost_model.txt
# java -jar ./RankLib-2.16.jar -load ./rank_model/kubernetes/kubernetes_RankBoost_model.txt -rank ./rank_data/kubernetes/kubernetes_svm_rank_format_year_data.txt -gmax 12 -norm zscore -indri ./rank_model/kubernetes/result/kubernetes_result_RankBoost.txt
